Distance and signed distance
export distance, signed_distanceWhat is distance? What is signed distance?
Distance is the distance of a point to another geometry. This is always a positive number. If a point is inside of geometry, so on a curve or inside of a polygon, the distance will be zero. Signed distance is mainly used for polygons and multipolygons. If a point is outside of a geometry, signed distance has the same value as distance. However, points within the geometry have a negative distance representing the distance of a point to the closest boundary. Therefore, for all "non-filled" geometries, like curves, the distance will either be postitive or 0.
To provide an example, consider this rectangle:
import GeometryOps as GO
import GeoInterface as GI
using Makie
using CairoMakie
rect = GI.Polygon([[(0,0), (0,1), (1,1), (1,0), (0, 0)]])
point_in = (0.5, 0.5)
point_out = (0.5, 1.5)
f, a, p = poly(collect(GI.getpoint(rect)); axis = (; aspect = DataAspect()))
scatter!(GI.x(point_in), GI.y(point_in); color = :red)
scatter!(GI.x(point_out), GI.y(point_out); color = :orange)
fThis is clearly a rectangle with one point inside and one point outside. The points are both an equal distance to the polygon. The distance to point_in is negative while the distance to point_out is positive.
(
GO.distance(point_in, rect), # == 0
GO.signed_distance(point_in, rect), # < 0
GO.signed_distance(point_out, rect) # > 0
)(0.0, -0.5, 0.5)Consider also a heatmap of signed distances around this object:
xrange = yrange = LinRange(-0.5, 1.5, 300)
f, a, p = heatmap(xrange, yrange, GO.signed_distance.(Point2f.(xrange, yrange'), Ref(rect)); colormap = :RdBu, colorrange = (-0.75, 0.75))
a.aspect = DataAspect(); Colorbar(f[1, 2], p, label = "Signed distance"); lines!(a, GI.convert(GO.GeometryBasics, rect)); f